package com.example.demo.controller;

import com.example.demo.model.Student;
import com.example.demo.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.demo.service.StudentService;


import java.util.Optional;
import java.util.List;

@Controller
@RequestMapping("student")
public class StudentController {
    @Autowired

    private StudentService studentService;
   // @Autowired
    //private StudentRepository studentRepository;

    @RequestMapping("list")
    public String list(ModelMap modelMap) {
        modelMap.put("studentList", studentService.list());
        //modelMap.put("studentList", studentRepository.findAll());
        return "student/list";
    }

    @RequestMapping("delete/{id}")
    public String delete(@PathVariable Integer id) {
        //studentRepository.deleteById(id);
        studentService.delete(id);
        return "redirect:/student/list";
    }

    @RequestMapping("save")
    public String save(Student s, ModelMap modelMap){
      /*  if (studentRepository.findByTel(s.getTel()).size() > 0) {

            return "error";

        }
        studentRepository.save(s);*/
        try {
            studentService.save(s);
            return "redirect:/student/list";
        } catch (Exception e) {
            modelMap.addAttribute("message", e.getMessage());
            return "error";
        }
    }

    @RequestMapping("edit/{id}")
    public String edit(@PathVariable Integer id, ModelMap map) {
        //Optional<Student> student = studentRepository.findById(id);
        //student.ifPresent(s -> map.put("student", s));
        map.put("student", studentService.get(id));
        return "student/edit";
    }

    @RequestMapping("update")
    public String update(Student s, ModelMap modelMap){
        /*List<Student> students = studentRepository.findByTel(s.getTel());

        if (students.size() > 1) {

            return "error";

        } else if (students.size() == 1) {

            if (!students.get(0).getId().equals(s.getId())) {

                return "error";

            }

        }*/
        try {
            studentService.update(s);
            return "redirect:/student/list";
        } catch (Exception e) {
            modelMap.addAttribute("message", e.getMessage());
            return "error";
        }}
   /* @RequestMapping(value = "search", method = RequestMethod.POST)  // 改为POST方法
    public String search(String name, ModelMap modelMap) {  // 参数改为name
        List<Student> studentList;
        if (name != null && !name.trim().isEmpty()) {
            // 使用 findByNameContaining 方法
            studentList = studentRepository.findByNameContaining(name.trim());
        } else {
            // 如果关键词为空，返回所有学生
            studentList = studentRepository.findAll();
        }
        modelMap.put("studentList", studentList);
        return "student/list";
    }
    */
   @RequestMapping("search")

   public String search(Student s, ModelMap modelMap) {

       modelMap.put("studentList", studentService.search(s));

       return "student/list";

   }
}